package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.BarChartData;
import com.itheima.common.PageBean;
import com.itheima.dto.CustomerDto;
import com.itheima.mapper.CustomerMapper;
import com.itheima.pojo.Customer;
import com.itheima.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 检查项分页条件查询
     *
     * @param customerDto 条件
     * @return PageBean
     */
    @Override
    public PageBean pageQuery(CustomerDto customerDto) {
         PageHelper.startPage(customerDto.getPage(),customerDto.getSize());
       Page<Customer> page= customerMapper.pageQuery(customerDto);
       return new PageBean(page.getTotal(),page.getResult());
    }

    /**
     * 会员年龄统计
     *
     * @return BarChartData
     */
    @Override
    public BarChartData ageReport() {
        List<Map<String,Object>> maps = customerMapper.ageReport();
        List<Object> ageGroupList = maps.stream().map(map-> map.get("aa")).collect(Collectors.toList());
        List<Object> ageCountList = maps.stream().map(map-> map.get("num")).collect(Collectors.toList());
        BarChartData barChartData = new BarChartData();
        barChartData.setAgeGroupList(ageGroupList);
        barChartData.setAgeCountList(ageCountList);
        return barChartData;
    }
}
